package org.zhang.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.zhang.shortlink.project.dao.entity.LinkLocaleStatsDO;
import org.zhang.shortlink.project.dto.req.ShortLinkStatsReqDTO;

import java.util.List;

/**
 * @author zhang
 * @description 针对表【t_link_locale_stats】的数据库操作Mapper
 * @createDate 2024-04-24 17:15:30
 * @Entity org.zhang.shortlink.project.dao.entity.LinkLocaleStatsDO
 */
@Mapper
public interface LinkLocaleStatsMapper extends BaseMapper<LinkLocaleStatsDO> {

    @Insert("""
            INSERT INTO t_link_locale_stats (id,full_short_url,date,cnt,province,city,adcode,country,create_time,update_time,del_flag)
            VALUES (#{id},#{fullShortUrl}, #{date}, #{cnt}, #{province},#{city},#{adcode},#{country},#{createTime},#{updateTime},#{delFlag})
            ON DUPLICATE KEY UPDATE cnt = cnt + #{cnt};
            """)
    void shortLinkStats(LinkLocaleStatsDO linkLocaleStatsDO);

    /**
     * 根据短链接获取指定日期内地区监控数据
     */
    @Select("SELECT " +
            "    province, " +
            "    SUM(cnt) AS cnt " +
            "FROM " +
            "    t_link_locale_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, province;")
    List<LinkLocaleStatsDO> listLocaleByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);
}




